package com.caimiao.miao.page.home;

import android.content.Context;
import android.support.v4.app.FragmentManager;

import com.caimiao.miao.adapter.SimpleFragmentPagerAdapter;
import com.caimiao.miao.interfaces.BaseActivity;
import com.caimiao.miao.interfaces.BaseFragmentImpl;
import com.caimiao.miao.interfaces.BasePresenterImpl;
import com.caimiao.miao.library.utils.tip.LogUtil;
import com.caimiao.miao.page.home.page.IPageContact;
import com.caimiao.miao.page.home.page.PageFragment;
import com.caimiao.miao.page.home.page.PagePresenter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public class HomePresenter extends BasePresenterImpl<IHomeContact.View, IHomeContact.Model> implements IHomeContact.Presenter {
    public HomePresenter(IHomeContact.View mView) {
        super(mView);
    }

    @Override
    protected IHomeContact.Model getModel() {
        return new HomeModel();
    }

    @Override
    public void start(Context context) {
        List<String> dataList = mModel.getAdverts(context);
        int size = dataList == null ? 0 : dataList.size();
        List<BaseFragmentImpl> fragments = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            fragments.add(getFragment(dataList.get(i)));
        }
        if (size > 1) {
            fragments.add(0, getFragment(dataList.get(size - 1)));
            fragments.add(getFragment(dataList.get(0)));
        }
        FragmentManager fm = ((BaseActivity) context).getSupportFragmentManager();
        mView.setAdapter(new SimpleFragmentPagerAdapter(fm, fragments));
    }

    private BaseFragmentImpl getFragment(String uri) {
        PageFragment fragment = new PageFragment();
        IPageContact.Presenter presenter = new PagePresenter(fragment);
        presenter.setAdvert(uri);
        return fragment;
    }


    @Override
    public void setCurrentItem(int index) {
        mModel.setCurrentItem(index);
    }

    @Override
    public void start() {
        Disposable disposable = Observable.timer(mModel.getIntervalTime(), TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(aLong -> mView.setCurrentItem(mModel.getNextItem()));
        addDisposable(disposable);
    }

    @Override
    public void stop() {
        unDisposable();
    }

    @Override
    public void setLogic(Context context, int width, int height) {
        if (width <= 0) width = 250;
        if (height <= 0) height = 250;
        LogUtil.d("Advert", "width=" + width + ", height=" + height);
        mView.setQRCode(mModel.getQrCode(context, width, height));
    }

    @Override
    public void startTimingShow() {
        Disposable disposable = Observable.timer(15 * 1000, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(aLong -> mView.setHideQrCode());
    }

    @Override
    public void addTouch(int x, int y) {
        mModel.addTouch(x, y);
    }
}
